<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

if (reqVar('azione') == 'salva' || reqVar('azione')== 'reset') {
	$msgPag = salvaSez();
} 

$sqlSel = "SELECT a.*, r.data_rif, r.descr  FROM rgl_rating r "
		." INNER JOIN rgl_azienda a ON a.id_azienda = r.id_azienda " 
		." WHERE r.id_rating = ".reqVarAS('id');
$objDB->query($sqlSel,'selRec');
if (!$acAnag = $objDB->fetch('selRec')) {
	backOnErr("rqueIns az - var. id '".reqVar('id')."' mancante o errato");
}

$sqlSel = "SELECT mq.*  FROM rgl_rating r "
		." INNER JOIN rgl_mod_rglob mg ON mg.id_mod_rglob = r.id_mod_rglob " 
		." INNER JOIN rgl_mod_rque mq ON mq.id_mod_rque = mg.id_mod_rque " 
		." WHERE r.id_rating = ".reqVarAS('id');
$objDB->query($sqlSel,'selRec');
if (!$acMod = $objDB->fetch('selRec')) {
	backOnErr("rqueIns mod - var. id '".reqVar('id')."' mancante o errato");
}

//lettura risposte memorizzate
$aQueRisp = array();
$sSql = "SELECT rq.str_que  FROM rgl_ratque rq " 
		." WHERE rq.id_rating = ".reqVar('id');
$objDB->query($sSql,'selRec');
$acRQ = $objDB->fetch('selRec');
if ($acRQ && !empty($acRQ['str_que'])) {
	$aQueRisp = unsgzu($acRQ['str_que']);
}

$sSql = "SELECT sq.* FROM rgl_mod_rque_sez sq "
	." WHERE sq.id_mod_rque = ".$acMod['id_mod_rque']
	." ORDER BY sq.seq ";
$objDB->query($sSql,'selSezVis');
$acSQ = $objDB->fetch('selSezVis');
reqDef('id_sez_rque',$acSQ['id_sez_rque']);

$sSql = "SELECT dq.*, sq.cod_sez_rque, sq.peso AS sq_peso, sq.descr FROM rgl_mod_rque_sez sq "
	." INNER JOIN rgl_mod_rque_dom dq ON dq.id_sez_rque = sq.id_sez_rque "
	." WHERE sq.id_sez_rque = ".reqVarInt('id_sez_rque')
	." ORDER BY dq.seq ";
$objDB->query($sSql,'selDom');

$sCmdR = '';
$sCmdR .= "<a class='cmd1' href=\"javascript:submAz('salva');\">registra sezione</a>";
$sCmdR .= '&nbsp;&nbsp;&nbsp;';
$sCmdR .= "<a class='cmd1' href=\"javascript:submAz('reset');\">reset sezione</a>";
$sCmdR .= '&nbsp;&nbsp;&nbsp;';
$sCmdR .= "<a class='cmd1' href=\"rqueVis.php?id=".reqvar('id')."&xsn=".XSN."\" >visualizza</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('', $msgPag, 'ratg');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
function setSez(sez) {
	document.frm1.id_sez_rque.value = sez;
	document.frm1.azione.value = 'reload';
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('id_sez_rque');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif(25);?>
   <tr>
      <td class="tit1" colspan='25' align='center'>Questionario</td>
   </tr>
   </tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Rating</td>
		<td class='txt1' colspan='1'>
			<?=reqVar('id')?>
		</td>
      <td class="txt1" colspan='3' align='center'>
      	<?=YMD2DMY($acAnag['data_rif'])?>
      </td>
      <td class="txt1" colspan='17' align='left'>
      	<?=$acAnag['descr']?>
      </td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Ragione sociale</td>
		<td class='txt1' colspan='14'>
			<?=$acAnag['rag_soc']?>
		</td>
		<td class='lab1' colspan='4' valign='top'>Codice fiscale</td>
		<td class='txt1' colspan='3'>
			<?=$acAnag['cod_fisc']?>
		</td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Sede</td>
		<td class='txt1' colspan='14'>
			<?=$acAnag['sede']?>
		</td>
		<td class='lab1' colspan='4' valign='top'>Codice CCIAA</td>
		<td class='txt1' colspan='3'>
			<?=$acAnag['cod_cciaa']?>
		</td>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Cod. settore</td>
		<td class='txt1' colspan='21'>
			*** - Settore generico&nbsp;
		</td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Sezioni</td>
		<td class='txt1' colspan='21'>
<?
$acDom = $objDB->fetch('selDom');
$nX = 0;
$nS = '?';
while($acSQ) {
	$nX++;
	if (isset($aQueRisp[$acSQ['cod_sez_rque']]['c']) && $aQueRisp[$acSQ['cod_sez_rque']]['c']=='S') {
		echo "[X] - ";
	} else {
		echo "[_] - ";
	}
	$sStyle = '';
	if ($acSQ['id_sez_rque']==reqVar('id_sez_rque')) {
   	echo getHtmlHidden('cod_sez_rque',$acSQ['cod_sez_rque']);
   	echo getHtmlHidden('des_sez',$acSQ['descr']);
   	echo getHtmlHidden('peso_sez',$acSQ['peso']);	
		$nS = $nX;
		$sStyle = 'color:'.RGL_COLORE_ROSSO.';';
	}
	echo "<a href=\"javascript:setSez('".$acSQ['id_sez_rque']."')\" class='txt1' style='$sStyle'>"
		.$nX.". ".$acSQ['descr']."</a><br/>\n";
	$acSQ = $objDB->fetch('selSezVis');
}
?>
		</td>
	</tr>
   <tr>
   <tr>
      <td class="tit1" colspan='25' align='left'> 
      	<? 
      	echo "Sezione $nS. ".$acDom['descr']
      	?>
      </td>
   </tr>
   <tr>
      <td class="lab1" colspan='1' align='center'>Cod.</td>
      <td class="lab1" colspan='15' align='center'>Domanda</td>
      <td class="lab1" colspan='9' align='center'>Risposta</td>
   </tr>
<?

$aOptRisp = array('aCapo'=>true);
$nd=0;
while($acDom) {
	$nd++;
?>
   <tr>
      <td class="txt1" colspan='1' align='left' valign='top'>
      	<?
      	echo $acDom['cod_dom_rque'];
		echo getHtmlHidden('domanda_'.$nd,$acDom['cod_dom_rque']);
      	echo getHtmlHidden('des_dom_'.$nd,$acDom['domanda']);
      	echo getHtmlHidden('peso_dom_'.$nd,$acDom['peso']);
      	?>
      </td>
      <td class="txt1" colspan='15' align='left' valign='top'>
      	<?
      	echo $acDom['domanda'];
      	?>
      </td>
      <td class="txt1" colspan='9' align='left'>
      	<? 
      	$aRisp = unserialize($acDom['risposte']);
			if (isset($aQueRisp[$acDom['cod_sez_rque']]['a'][$acDom['cod_dom_rque']])) {
				$rispX = $aQueRisp[$acDom['cod_sez_rque']]['a'][$acDom['cod_dom_rque']]['k'];
			} else {
				$rispX = count($aRisp)-1;
			}
			foreach($aRisp as $kX => $aX) {
      		$aRisp[$kX] = $aX[0];
				echo getHtmlHidden('des_risp_'.$nd.'_'.$kX,$aX[0]);
				echo getHtmlHidden('punt_risp_'.$nd.'_'.$kX,$aX[1]);
      	}
		echo getHtmlRadio('risposta_'.$nd,$aRisp, $rispX, $aOptRisp)."<br/>";
		echo getHtmlHidden('default_'.$nd,count($aRisp)-1);
      	?> 
      </td>
   </tr>
<?
	$acDom = $objDB->fetch('selDom');
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();

function salvaSez() {
	global $objDB, $objSess;
	$msg = '';
	
	//lettura risposte memorizzate
	$aQueRisp = array();
	$sqlSel = "SELECT rq.str_que, rq.id_mod_rque  FROM rgl_ratque rq " 
			." WHERE rq.id_rating = ".reqVar('id');
	$objDB->query($sqlSel,'selRec');
	$acRQ = $objDB->fetch('selRec');
	if ($acRQ && !empty($acRQ['str_que'])) {
		$aQueRispX = unsgzu($acRQ['str_que']);
	}
	$sqlSel = "SELECT *  FROM rgl_mod_rque_sez " 
			." WHERE id_mod_rque = ".intval($acRQ['id_mod_rque'])
			." ORDER BY seq ";
	$objDB->query($sqlSel,'selRec');
	while($acSQ = $objDB->fetch('selRec')) {
		if (isset($aQueRispX[$acSQ['cod_sez_rque']])) {
			$aQueRisp[$acSQ['cod_sez_rque']] = $aQueRispX[$acSQ['cod_sez_rque']];
			$aQueRisp[$acSQ['cod_sez_rque']]['d'] = $acSQ['descr'];
		} else {
			$aQueRisp[$acSQ['cod_sez_rque']]['d'] = $acSQ['descr'];
			$aQueRisp[$acSQ['cod_sez_rque']]['p'] = $acSQ['peso'];
			$aQueRisp[$acSQ['cod_sez_rque']]['a'] = array();
			$aQueRisp[$acSQ['cod_sez_rque']]['c'] = 'N';
		}
	}
	unset($aQueRisp[reqVar('cod_sez_rque')]);
	
	$aQueRisp[reqVar('cod_sez_rque')]['d'] = reqVar('des_sez');
	$aQueRisp[reqVar('cod_sez_rque')]['p'] = reqVar('peso_sez');
	$aQueRisp[reqVar('cod_sez_rque')]['c'] = (reqVar('azione')=='salva')?'S':'N';
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,9)!='risposta_') continue;
		$suff = substr($nX,9);
		$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['d'] = reqVar('des_dom_'.$suff);
		if (reqVar('azione')=='salva') {
			$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['k'] = $vX;
		} else {
			$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['k'] = reqVar('default_'.$suff);
		}
		$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['r'] = reqVar('des_risp_'.$suff.'_'.$vX);
		$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['v'] = reqVar('punt_risp_'.$suff.'_'.$vX);
		$aQueRisp[reqVar('cod_sez_rque')]['a'][reqVar('domanda_'.$suff)]['p'] = reqVar('peso_dom_'.$suff);
	}
	//dbg_($aQueRisp);
	$sqlSel = "UPDATE rgl_ratque SET str_que = '".addslashes(gzcser($aQueRisp))."' " 
			.", sigla = '', punteggio = 0"
			.", data_ora = '".date('Y-m-d H:i:s')."'"
			.", id_utente = ".$objSess->idUtente
			." WHERE id_rating = ".reqVar('id');
	$objDB->query($sqlSel,'selRec');
	$sqlSel = "UPDATE rgl_rating SET sigla_calc = '', sigla = '', punteggio = 0"
			.", data_ora = '".date('Y-m-d H:i:s')."'"
			.", id_utente = ".$objSess->idUtente
			." WHERE id_rating = ".reqVar('id');
	$objDB->query($sqlSel,'selRec');
	
	return (reqVar('azione')=='salva')?'sezione registrata':'sezione resettata';
}
	